package com.deviche.taoke;

import android.util.Log;
import com.deviche.taoke.bean.Goods;
import com.deviche.taoke.common.Constant;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.util.ArrayList;

/**
 * Created by h on 2018/4/20.
 */

public class GoodsPaser {
    //参考博客 https://www.jianshu.com/p/a0ff51600b18
    /**
     * 网页中 product-img 为一个商品条目标签单元
     */

    public static final String TAG = GoodsPaser.class.getSimpleName();

    private static ArrayList<Goods> goodsList = new ArrayList<>() ;
    public static ArrayList<Goods> parseFrom(String url) {
        try {
            Goods goods = new Goods();
            //从一个URL加载一个Document对象。
            Document doc = Jsoup.connect(url).get();

            Elements elements = doc.select("div.product-img");
            Elements elements2 = doc.select("div.copybox");
            Elements elements3 = doc.select("div.product-price");
            Elements elements4 = doc.select("div.quan-infro");
            Elements elements5 = doc.select("div.sales-infro");
            Log.e(TAG, "parseFrom: "+goodsList.size());
            Log.e(TAG, "parseFrom: -------------------------------------------"+elements.size() );
            for (int i = 0; i < elements.size(); i++) {
                Log.e(TAG, "parseFrom: " + elements.get(i).select("a").attr("href"));
                String tkzs = elements.get(i).select("a").attr("href");
                goods.setTkzs(Constant.HOST+tkzs);
                //注意图片有两种标签 否则可能拿不到
                String img = elements.get(i).select("img").attr("src");
                if (!"".equals(img)){
                    Log.e(TAG, "parseFrom: " + elements.get(i).select("img").attr("src"));
                    img = elements.get(i).select("img").attr("src");
                }else {
                    Log.e(TAG, "parseFrom: " + elements.get(i).select("img").attr("data-original"));
                    img = elements.get(i).select("img").attr("data-original");
                }
                goods.setImg(img);
                Log.e(TAG, "parseFrom: " + elements.get(i).select("img").attr("alt"));
                String title = elements.get(i).select("img").attr("alt");
                goods.setTitle(title);

                String taobao = elements2.get(i).select("a").attr("href");
                goods.setTaobao(taobao);

                String price = elements3.get(i).select("span").text();
                goods.setPrice(price);

                String coupon = elements4.get(i).select("a").attr("href");
                goods.setCoupon(coupon);
                String money = elements4.get(i).select("span").text();
                goods.setMoney(money);

                String sales = elements5.get(i).select("p").text();
                goods.setSales(sales);
                goodsList.add(goods);
            }
            Log.e(TAG, "parseFrom11111111111111111111111111111: "+goodsList.size());

           /* Elements elements2 = doc.select("div.copybox");
            Log.e(TAG, "parseFrom2: -------------------------------------------"+elements2.size() );
            for (int i = 0; i < elements2.size(); i++) {
                Log.e(TAG, "parseFrom2: " + elements2.get(i).select("a").attr("href"));
                String taobao = elements2.get(i).select("a").attr("href");
                goods.setTaobao(taobao);
                Log.e(TAG, "parseFrom2: " + elements2.get(i).select("a").attr("data-coupon-id"));
            }

            Elements elements3 = doc.select("div.product-price");
            Log.e(TAG, "parseFrom3: -------------------------------------------"+elements3.size() );
            for (int i = 0; i <elements3.size() ; i++) {
                Log.e(TAG, "parseFrom3: " + elements3.get(i).select("span").text());
                String price = elements3.get(i).select("span").text();
                goods.setPrice(price);
            }
            //优惠券地址 优惠券大小 券后价 佣金与佣金比例
            Elements elements4 = doc.select("div.quan-infro");
            Log.e(TAG, "parseFrom4: -------------------------------------------"+elements4.size() );
            for (int i = 0; i <elements4.size(); i++) {
                Log.e(TAG, "parseFrom4: " + elements4.get(i).select("a").attr("href"));
                String coupon = elements4.get(i).select("a").attr("href");
                goods.setCoupon(coupon);
                Log.e(TAG, "parseFrom4: " + elements4.get(i).select("span").text());
                String money = elements4.get(i).select("span").text();
                goods.setMoney(money);
            }
            //销量
            Elements elements5 = doc.select("div.sales-infro");
            Log.e(TAG, "parseFrom5: -------------------------------------------"+elements5.size() );
            for (int i = 0; i <elements5.size(); i++) {
                Log.e(TAG, "parseFrom5: " + elements5.get(i).select("p").text());
                String sales = elements5.get(i).select("p").text();
                goods.setSales(sales);
            }

            //添加到集合里面
            goodsList.add(goods);
            Log.e(TAG, "parseFrom11111111111111111111111111111: "+goodsList.size());*/
            return goodsList;
        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }
        return null;
    }
}
